const app = getApp();

import {
  getPlaceInfo
} from '../../utils/util';
import {
  post
} from '../../utils/request';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    privacyPolicy: null,
    modalVisible: false,
    code: "",

    config: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    wx.setBackgroundColor({
      backgroundColor: "#ffffff"
    });

    this.wxlogin();
  },

  // 获取手机号
  getPhoneNumber(e) {
    let that = this;
    let {
      detail
    } = e;
    if (detail.code) {
      wx.checkSession({
        success() {
          app.bindPhoneByCode(detail.code, that.data.code, () => {
            that.wxlogin();
          })
        },
        fail() {
          that.wxlogin();
        }
      })
    } else if (detail.errMsg === 'getPhoneNumber:ok') {
      wx.checkSession({
        success() {
          app.bindPhoneNumber(detail, that.data.code, () => {
            that.wxlogin();
          });
        },
        fail() {
          that.wxlogin();
        }
      })
    }
  },

  // 获取用户登录凭证（有效期五分钟）
  wxlogin() {
    wx.login({
      success: (res) => {
        if (res.errMsg !== 'login:ok') {
          return;
        }
        this.setData({
          code: res.code
        });
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    app.getConfig(config => {
      this.setData({
        config
      });
    });

    post(`/common/place/${app.globalData.tenantCode}/MP_SVIP_POLICY`)
      .then(resp => {
        if (resp.code === 0) {
          this.setData({
            privacyPolicy: getPlaceInfo(resp.data, 'MP_SVIP_POLICY')
          });
        }
      });
  },

  // 协议点击监听
  onAgree() {
    this.setData({
      checked: !this.data.checked
    });
  },

  // 同意
  onYes() {
    this.setData({
      checked: true,
      modalVisible: false
    });
  },

  // 不同意
  onNo() {
    this.setData({
      checked: false,
      modalVisible: false
    });
  },

  // 弹出框
  onModalVisible() {
    this.setData({
      modalVisible: !this.data.modalVisible
    });
  },


});